package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IEmpContactsRequireDAO;
import org.jsola.hr.dto.EmpContactsRequireAddDTO;
import org.jsola.hr.dto.EmpContactsRequireUpdateDTO;
import org.jsola.hr.entity.CompanyDO;
import org.jsola.hr.entity.EmpContactsRequireDO;
import org.jsola.hr.query.EmpContactsRequireQuery;
import org.jsola.hr.service.ICompanyService;
import org.jsola.hr.service.IEmpContactsRequireService;
import org.jsola.hr.vo.EmpContactsRequireListVO;
import org.jsola.hr.vo.EmpContactsRequireVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrEmpContactsRequireServiceImpl")
public class EmpContactsRequireServiceImpl implements IEmpContactsRequireService {

    @Autowired
    private IEmpContactsRequireDAO empContactsRequireDAO;

    @Autowired
    private ICompanyService companyService;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpContactsRequireVO save(EmpContactsRequireAddDTO empContactsRequireAddDTO, TokenUser tokenUser) {
        // 校验是否存在
        EmpContactsRequireVO requireVO = findByCompanyAndType(
                empContactsRequireAddDTO.getCompanyId(),
                empContactsRequireAddDTO.getRequireType(),
                tokenUser.getSiteId());
        if (!Objects.isNull(requireVO)) {
            // 修改
            EmpContactsRequireUpdateDTO requireUpdateDTO = empContactsRequireAddDTO.to(EmpContactsRequireUpdateDTO.class);
            requireUpdateDTO.setId(requireVO.getId());

            update(requireUpdateDTO, tokenUser);
            return selectById(requireVO.getId(), tokenUser.getSiteId());
        }
        // 转DO
        EmpContactsRequireDO empContactsRequireDO = empContactsRequireAddDTO.to(EmpContactsRequireDO.class);
        // 保存
        empContactsRequireDO = save(empContactsRequireDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return empContactsRequireDO.to(EmpContactsRequireVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(EmpContactsRequireUpdateDTO empContactsRequireUpdateDTO, TokenUser tokenUser) {
        //转DO
        EmpContactsRequireDO empContactsRequireDO = empContactsRequireUpdateDTO.to(EmpContactsRequireDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(empContactsRequireDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... empContactsRequireIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) empContactsRequireIds);
    }


    @Override
    public EmpContactsRequireVO selectById(String empContactsRequireId, String siteId) {
        EmpContactsRequireDO empContactsRequireDO = selectDOById(empContactsRequireId, siteId);
        if (empContactsRequireDO == null) {
            return null;
        }
        return empContactsRequireDO.to(EmpContactsRequireVO.class);
    }

    @Override
    public List<EmpContactsRequireListVO> select(EmpContactsRequireQuery empContactsRequireQuery, String siteId) {
        List<EmpContactsRequireDO> empContactsRequireDOList = selectDO(empContactsRequireQuery, siteId);
        if(CollectionUtils.isEmpty(empContactsRequireDOList)) {
            return empContactsRequireDOList == null ? null : new ArrayList<>();
        }
        return empContactsRequireDOList.stream()
                .map(empContactsRequireDO -> empContactsRequireDO.to(EmpContactsRequireListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(EmpContactsRequireQuery empContactsRequireQuery, String siteId) {
        Example example = buildExample(empContactsRequireQuery,siteId);
        return empContactsRequireDAO.selectCountByExample(example);
    }

    @Override
    public Page<EmpContactsRequireListVO> selectPage(EmpContactsRequireQuery empContactsRequireQuery, String siteId) {
        Example example = buildExample(empContactsRequireQuery,siteId);
        Page<EmpContactsRequireDO> page = empContactsRequireDAO.selectPageByExample(example,
                empContactsRequireQuery.getPageNo(),
                empContactsRequireQuery.getPageSize());

        return page.to(EmpContactsRequireListVO.class);
    }

    @Override
    public EmpContactsRequireDO selectDOById(String empContactsRequireId, String siteId) {
        return listById(empContactsRequireId, siteId);
    }

    @Override
    public List<EmpContactsRequireDO> selectDO(EmpContactsRequireQuery empContactsRequireQuery, String siteId) {
        Example example = buildExample(empContactsRequireQuery,siteId);
        return empContactsRequireDAO.selectByExample(example);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpContactsRequireVO selectByCompanyAndType(String companyId, String requireType, TokenUser tokenUser) {
        EmpContactsRequireVO requireVO = findByCompanyAndType(companyId, requireType, tokenUser.getSiteId());
        if (Objects.isNull(requireVO)) {
            CompanyDO company = companyService.selectDOById(companyId, tokenUser.getSiteId());
            if (Objects.isNull(company)) {
                return null;
            }
            // 没有初始化一条
            EmpContactsRequireAddDTO requireAddDTO = new EmpContactsRequireAddDTO();
            requireAddDTO.setCompanyId(companyId);
            requireAddDTO.setRequireType(requireType);

            requireVO = save(requireAddDTO, tokenUser);
        }
        return requireVO;
    }

    /**
     * 根据公司id和类型查询所需信息
     *
     * @param companyId   公司id
     * @param requireType 类型
     * @param siteId      站点
     * @return 所需信息
     */
    private EmpContactsRequireVO findByCompanyAndType(String companyId, String requireType, String siteId) {
        Example example = new Example(EmpContactsRequireDO.class);
        example.and()
                .andEqualTo("companyId", companyId)
                .andEqualTo("requireType", requireType)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);

        EmpContactsRequireDO requireDO = empContactsRequireDAO.selectOneByExample(example);
        if (Objects.isNull(requireDO)) {
            return null;
        }
        return requireDO.to(EmpContactsRequireVO.class);
    }


    /**
     * 根据查询参数，构建example

     * @param empContactsRequireQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(EmpContactsRequireQuery empContactsRequireQuery, String siteId) {
        Example example = new Example(EmpContactsRequireDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (empContactsRequireQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", empContactsRequireQuery.getCompanyId());
        }
        if (empContactsRequireQuery.getRequireType() != null) {
            example.and().andEqualTo("requireType", empContactsRequireQuery.getRequireType());
        }
        if (empContactsRequireQuery.getName() != null) {
            example.and().andEqualTo("name", empContactsRequireQuery.getName());
        }
        if (empContactsRequireQuery.getRelation() != null) {
            example.and().andEqualTo("relation", empContactsRequireQuery.getRelation());
        }
        if (empContactsRequireQuery.getBirthday() != null) {
            example.and().andEqualTo("birthday", empContactsRequireQuery.getBirthday());
        }
        if (empContactsRequireQuery.getPhone() != null) {
            example.and().andEqualTo("phone", empContactsRequireQuery.getPhone());
        }
        if (empContactsRequireQuery.getCompany() != null) {
            example.and().andEqualTo("company", empContactsRequireQuery.getCompany());
        }
        if (empContactsRequireQuery.getPosition() != null) {
            example.and().andEqualTo("position", empContactsRequireQuery.getPosition());
        }
        if (empContactsRequireQuery.getPoliticalStatus() != null) {
            example.and().andEqualTo("politicalStatus", empContactsRequireQuery.getPoliticalStatus());
        }
        // 排序
        ExampleKit.setExampleOrder(example,empContactsRequireQuery.getOrders());
        return example;
    }
}




